/**
 * Class: FPoint2
 *
 * Purpose: 
 *
 * Created: Jan 23, 2010 @ 11:06:19 PM
 * @author Ken Scott
 */
package org.openfantasia.util.math;

import org.openfantasia.util.Copyable;

public class FPoint2 implements Copyable<FPoint2> {

	private static final int N = 2;

	public float p[];


	public FPoint2() {
		p = new float[N];
	}


	public FPoint2(float p0, float p1) {
		p = new float[]{p0, p1};
	}


	public FPoint2(float[] p) {
		if (p.length != N) {
			throw new IllegalArgumentException("Input array must be of length " + N + ", not " + p.length);
		}
		this.p = new float[N];
		for (int i = 0; i < p.length; i++) {
			this.p[i] = p[i];
		}
	}


	public FPoint2(FPoint2 p) {
		this(p.p);
	}


	public FPoint2 copy() {
		return new FPoint2(p);
	}

	public void copy(FPoint2 that) {
		p = Math1D.copy(that.p);
	}



	public FPoint2 clear() {
		Math1D.fill(p, 0);
		return this;
	}


	public float[] getValue() {
		return p;
	}


	public float dotp(FPoint2 other) {
		return Math1D.dotp(this.p, other.p);
	}


	public FPoint2 normalize() {
		float mag = PMath.mag(p[0], p[1]);
		for (int i = 0; i < N; i++) {
			this.p[i] /= mag;
		}
		return this;
	}


	public FPoint2 neg() {
		for (int i = 0; i < N; i++) {
			this.p[i] = -this.p[i];
		}
		return this;
	}


	public FPoint2 abs() {
		for (int i = 0; i < N; i++) {
			this.p[i] = Math.abs(this.p[i]);
		}
		return this;
	}


	public FPoint2 add(float... a) {
		if(a == null) {
			return this;
		}

		for (int i = 0; i < Math.min(N, a.length); i++) {
			this.p[i] += a[i];
		}
		return this;
	}


	public FPoint2 add(FPoint2 a) {
		if(a == null) {
			return this;
		}

		for (int i = 0; i < Math.min(N, a.p.length); i++) {
			this.p[i] += a.p[i];
		}
		return this;
	}


	public FPoint2 sub(float... a) {
		if(a == null) {
			return this;
		}

		for (int i = 0; i < Math.min(N, a.length); i++) {
			this.p[i] -= a[i];
		}
		return this;
	}


	public FPoint2 sub(FPoint2 a) {
		if(a == null) {
			return this;
		}

		for (int i = 0; i < Math.min(N, a.p.length); i++) {
			this.p[i] -= a.p[i];
		}
		return this;
	}


	public FPoint2 mul(float... a) {
		if(a == null) {
			return this;
		}

		for (int i = 0; i < Math.min(N, a.length); i++) {
			this.p[i] *= a[i];
		}
		return this;
	}


	public FPoint2 mul(FPoint2 a) {
		if(a == null) {
			return this;
		}

		for (int i = 0; i < Math.min(N, a.p.length); i++) {
			this.p[i] *= a.p[i];
		}
		return this;
	}


	public FPoint2 div(float... a) {
		if(a == null) {
			return this;
		}

		for (int i = 0; i < Math.min(N, a.length); i++) {
			this.p[i] /= a[i];
		}
		return this;
	}


	public FPoint2 div(FPoint2 a) {
		if(a == null) {
			return this;
		}

		for (int i = 0; i < Math.min(N, a.p.length); i++) {
			this.p[i] /= a.p[i];
		}
		return this;
	}



	public double[] toDouble() {
		return new double[]{p[0], p[1]};
	}


	public float[] toFloat() {
		return new float[]{(float) p[0], (float) p[1]};
	}


	public int[] toInt() {
		return new int[]{(int) p[0], (int) p[1]};
	}



	@Override
	public String toString() {
		return Math1D.toString(p);
	}
}